//
//  CompleteViewController.m
//  OperationPro
//
//  Created by yaxue wang on 2017/2/17.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "CompleteViewController.h"
#import "LoadingView.h"
@interface CompleteViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UITextView *txtView;

@end

@implementation CompleteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   self.title=@"故障备注";
    __block typeof(self) blockself = self;
    
    [self.navBar setBackAction:^{
        if(([_txtView.text isEqualToString:@"记录维修内容，以备后续跟踪..."]||[_txtView.text isEqualToString:@""]))
        {
            [blockself backController];
            
        }else
        {
            [CClass showAlertTitle:nil hidden:NO context:@"离开后内容丢失。是否离开？" oneBtn:@"离开" twoBtn:@"取消" handler:^{
                [blockself backController];
                
            }handler:^{
                
            }];
        }
        
    }];
    
    
    
 
    [self.navBar createRightBtn:@"保存" block:^{
        if (_txtView.text.length >200) {
            [CClass showMessage:@"您最多可以输入200个字"];
            return;
        }
        if ([_txtView.text isEqualToString:@"记录维修内容，以备后续跟踪..."]||[_txtView.text isEqualToString:@""]){
            [CClass showMessage:@"请填写故障备注！"];
            return;
            
        }
       
        [[LoadingView shareLoading] startAnimating];

        NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
        [dic setObject:[self.dataDic objectForKey:@"orderUuid"] forKey:@"orderUuid"];
        [dic setObject:_txtView.text forKey:@"content"];
        [dic setObject:[self.dataDic objectForKey:@"faultTypeUuid"] forKey:@"faultTypeUuid"];
        [dic setObject:[self.dataDic objectForKey:@"longitude"] forKey:@"longitude"];
        [dic setObject:[self.dataDic objectForKey:@"latitude"] forKey:@"latitude"];
        [CClass HttpRequestTask:YES withUrl:@"warning_orderCompleted" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
            [[LoadingView shareLoading] stopAnimating];

            [self.navigationController popToRootViewControllerAnimated:YES];


        } onError:^(BaseModel *model) {
            
        }];
       
        
    }];

  
 
    //输入框
    UIView *bgView =[[UIView alloc]initWithFrame:CGRectMake(15, 64, WindowWidth-30, 200)];
    bgView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:bgView];
    [bgView addSubview:self.txtView];
    _txtView = [[UITextView alloc] init];
    [_txtView setDelegate:self];
    [_txtView setText:@"记录维修内容，以备后续跟踪..."];
    [_txtView setTextColor:C(@"cbcbcb")];
    _txtView.font =F(11);
    [_txtView setTextContainerInset:UIEdgeInsetsMake(5, 0, 5, 0)];
    [bgView addSubview:_txtView];
    [self.txtView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).mas_offset(0);
        make.right.equalTo(bgView).mas_offset(0);
        make.top.equalTo(bgView).mas_offset(5);
        make.bottom.equalTo(bgView).mas_offset(-5);
    }];
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
//                                                name:@"UITextViewTextDidChangeNotification"
//                                              object:self.txtView];

}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@"记录维修内容，以备后续跟踪..."]) {
        [textView setText:@""];
        [textView setTextColor:C(@"000000")];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    //禁止输入emoji表情
 
    
    return YES;
    
}

- (void)textViewDidChange:(UITextView *)textView{
    
    if ([CClass stringContainsEmoji:textView.text]) {
        textView.text=[textView.text substringToIndex:textView.text.length-2];
        return;
    }
    if (textView.markedTextRange == nil && textView.text.length > 200)  //加上 textView.markedTextRange == nil判断，当此属性为nil时，代表不在这种联想输入等待确定状态。
    
    {
        
        textView.text = [textView.text substringToIndex:200];
        
    }
    
}

@end
